package com.lungle.warmdiary.app.view.active.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.BaseViewPagerAdapter;
import com.lungle.warmdiary.app.ui.tab.SlidingTabLayout;
import com.lungle.warmdiary.app.view.IPagerFragment;
import com.lungle.warmdiary.app.view.active.adapter.ActiveDetailTabPagerAdapter;


public class ActiveDetailPagerFragment extends Fragment implements
        IPagerFragment {

    private ViewPager mViewPager;
    private ActiveDetailTabPagerAdapter mTabAdapter;
    private SlidingTabLayout mSlidingTabLayout;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_viewpager3, container,
                false);

        mSlidingTabLayout = (SlidingTabLayout) getActivity().findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.tab_indicator_detail, R.id.tv_media_name);
        mViewPager = (ViewPager) view.findViewById(R.id.active_detail_tab_pager);
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));

        final Resources res = getResources();
        mSlidingTabLayout.setSelectedIndicatorColors(res.getColor(R.color.white));
        mSlidingTabLayout.setDistributeEvenly(true);

        if (mTabAdapter == null) {
            mTabAdapter = new ActiveDetailTabPagerAdapter(getChildFragmentManager());
        }
        mViewPager.setOffscreenPageLimit(mTabAdapter.getCount());
        mViewPager.setAdapter(mTabAdapter);

        mSlidingTabLayout.setViewPager(mViewPager);


        return view;
    }


    @Override
    public BaseViewPagerAdapter getPagerAdapter() {
        return mTabAdapter;
    }

    @Override
    public ViewPager getViewPager() {
        return mViewPager;
    }

    @Override
    public Fragment getCurrentFragment() {
        return mTabAdapter.getItemAt(mViewPager.getCurrentItem());
    }
}